package com.zhl.scheduling.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhl.scheduling.mapper.MachineMapper;
import com.zhl.scheduling.model.bo.MachineListQueryBO;
import com.zhl.scheduling.model.entity.Machine;
import com.zhl.scheduling.model.vo.ListVO;
import com.zhl.scheduling.model.vo.ResStatus;
import com.zhl.scheduling.model.vo.ResultVO;
import com.zhl.scheduling.service.MachineService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: ZhangLu In DLUT
 * @since: 1.0.0
 * @Description: 机器操作接口的实现类
 */
@Service
public class MachineServiceImpl implements MachineService {

    @Resource
    private MachineMapper machineMapper;

    /**
     * 根据条件查询相关的机器信息
     * @param machineListQueryBO 客户端封装的查询机器实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO queryByConditions(MachineListQueryBO machineListQueryBO) {

        Example example = new Example(Machine.class);
        Example.Criteria criteria = example.createCriteria();

        if (machineListQueryBO.getMachineId() != null && !machineListQueryBO.getMachineId().equals("")) {
            criteria.andEqualTo("machineId", machineListQueryBO.getMachineId());
        }
        if (machineListQueryBO.getMachineNo() != null && !machineListQueryBO.getMachineNo().equals("")) {
            criteria.andEqualTo("machineNo", machineListQueryBO.getMachineNo());
        }
        if (machineListQueryBO.getMachineName() != null && !machineListQueryBO.getMachineName().equals("")) {
            criteria.andLike("machineName", "%"+machineListQueryBO.getMachineName()+"%");
        }
        if (machineListQueryBO.getMachineStatus() != null && !machineListQueryBO.getMachineStatus().equals("")) {
            criteria.andEqualTo("machineStatus", machineListQueryBO.getMachineStatus());
        }
        if (machineListQueryBO.getOrderBy() != null && !machineListQueryBO.getOrderBy().equals("") ) {
            Example.OrderBy orderBy = example.orderBy(machineListQueryBO.getOrderBy().split(" ")[0]);
            if (machineListQueryBO.getOrderBy().split(" ")[1].equals("asc")){
                orderBy.asc();
            } else {
                orderBy.desc();
            }
        }
        Page<Machine> list = PageHelper.startPage(machineListQueryBO.getPage(), machineListQueryBO.getLimit()).doSelectPage(() -> machineMapper.selectByExample(example));
        if (list != null) {
            ListVO listVO = new ListVO();
            listVO.setItems(list);
            listVO.setTotal(PageHelper.count(() -> machineMapper.selectByExample(example)));
            return new ResultVO(ResStatus.OK,"获取列表成功", listVO);
        }
        return new ResultVO(ResStatus.NO,"获取列表失败", null);
    }

    /**
     * 根据客户端提供的机器信息更新相应机器
     * @param machine 客户端封装的更新机器实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO update(Machine machine) {
        machine.setUpdatedTime(new Date());
        int updateNum = machineMapper.updateByPrimaryKey(machine);
        if (updateNum > 0) {
            return new ResultVO(ResStatus.OK,"更新数据成功", null);
        }else {
            return new ResultVO(ResStatus.NO,"更新数据失败", null);
        }
    }

    /**
     * 根据客户端提供的机器信息添加相应机器
     * @param machine 客户端封装的添加机器实体类
     * @return ResultVO 用作封装数据给前端的数据
     */
    @Override
    public ResultVO add(Machine machine) {
        machine.setUpdatedTime(new Date());
        machine.setCreatedTime(new Date());
        int insertNum = machineMapper.insert(machine);
        if (insertNum > 0) {
            return new ResultVO(ResStatus.OK,"新增数据成功", null);
        }else {
            return new ResultVO(ResStatus.OK,"更新数据失败", null);
        }
    }
}
